#include "../server.hpp"

class EchoServer{
private:
    TcpServer _ser;
private:
    void OnClosed(const PtrConnection &conn){
    DEBUG_LOG("Close CONNECTION:%p:",conn.get());
}

void OnConnected(const PtrConnection &conn){
    DEBUG_LOG("NEW CONNECTION:%p:",conn.get());
}

//将收到的信息回显
void OnMessage(const PtrConnection &conn,Buffer*buf){
    conn->Send(buf->ReadPosition(),buf->ReadAbleSize());
    buf->MoveReadOffset(buf->ReadAbleSize());
    conn->Shutdown();
}
public:
    EchoServer(int port):_ser(port){
        _ser.SetConnectedCallback(std::bind(&EchoServer::OnConnected,this,std::placeholders::_1));
        _ser.SetMessageCallback(std::bind(&EchoServer::OnMessage,this,std::placeholders::_1,std::placeholders::_2));
        _ser.SetClosedCallback(std::bind(&EchoServer::OnClosed,this,std::placeholders::_1));
        _ser.SetThreadCount(2);
        _ser.CreateTHreadPool();
    }

    void Start(){
        _ser.Start();
    }

};